var express = require('express');
var router = express.Router();
var taskdao = require('../dao/taskdao');
var utils = require('../common/utils');

router.get('/fulltask', function (req, res, next) {
    res.render('task/list', {
        data: 1 //data=0当前用户下的任务  data=1所有用户的任务
    });
});

router.get('/mytask', function (req, res, next) {
    res.render('task/list', {
        data: 0 //data=0当前用户下的任务  data=1所有用户的任务
    });
});
router.get('/statistics', function (req, res, next) {
    res.render('task/statistics', {
        data: 0 //data=0当前用户下的任务  data=1所有用户的任务
    });
});
router.get('/addtask', function (req, res, next) {
    res.render('task/addtask');
});

router.get('/users', function (req, res, next) {
    res.render('task/users');
});

router.get('/baseupdate', function (req, res, next) {
    var taskid = req.query.taskid;
    taskdao.getbaseinfo(taskid).then(function (data) {
        data[0].description = encodeURIComponent(data[0].description);
        res.render('task/baseupdate', {
            data: data[0]
        });
    }, function (error) {
        res.render('500');
    })

});

router.get('/flowupdate', function (req, res, next) {
    var configid = req.query.configid;
    taskdao.getflowinfo(configid).then(function (data) {
        var userobj = JSON.stringify(data[1]);
        res.render('task/flowupdate', {
            data: data[0][0],
            users: userobj
        });
    }, function (error) {
        res.render('500');
    })
});

//创建的任务列表
router.get('/list', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var key = req.query.key || ''; //查询条件
    var isfull = req.query.full; //1=全部查询 0=按用户筛选
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        key: key,
        isfull: (isfull == '1' ? true : false),
        userid: req.session.user.userid
    };
    taskdao.getlist(condition).then(function (data) {
        res.json({
            "code": 0,
            "msg": "",
            count: data[1][0].total,
            data: data[0]
        });
    }, function (error) {
        res.json({
            "code": 1,
            "msg": error.message
        });
    })
});

//获取用户参与过的任务列表
router.get('/partake_list', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var key = req.query.key || ''; //查询条件
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        key: key,
        userid: req.session.user.userid
    };
    taskdao.partake_list(condition).then(function (data) {
        res.json({
            "code": 0,
            "msg": "",
            count: data[1][0].total,
            data: data[0]
        });
    }, function (error) {
        res.json({
            "code": 1,
            "msg": error.message
        });
    })
});

router.post('/addresource', function (req, res, next) {
    var resourcename = req.body.resourcename;
    var folderid = req.body.folderid;
    var resourceid = utils.generalUUID();

    var params = {
        folderid: folderid,
        resourcename: resourcename,
        resourceid: resourceid
    };

    taskdao.addresource(params).then(function (data) {
        console.log(data);

        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.get('/resourcelist', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var folderid = req.query.folderid || ''; //文件夹ID
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        folderid: folderid
    };
    taskdao.resourcelist(condition).then(function (data) {
        return res.json({
            code: 0,
            msg: '',
            count: data[1][0].total,
            data: data[0]
        });
    }, function (error) {
        return res.json({
            code: 1,
            msg: error.message
        });
    });
});

router.post('/addtask', function (req, res, next) {
    var taskname = req.body.taskname;
    var description = req.body.description;
    var taskid = utils.generalUUID();
    var userid = req.session.user.userid;
    var tasktags = req.body.tasktags;
    var exttags = req.body.exttags || '';
    var actions = req.body.actions;
    var task = req.body.task;

    var taskobj = JSON.parse(task);
    var configid = utils.generalUUID();

    var dsid = utils.generalUUID(); //新任务对应的dataset副本

    var dslist_params = {
        dsid: dsid,
        dsname: 'ds-' + taskname,
        dstags: tasktags,
        dstype: 1,
        status: 0,
        dstable: 'dstable_' + dsid,
        userid: req.session.user.userid,
        description: '',
        createtype: 0,
        inputdsids: taskobj.datasetid,
        actions: actions
    };

    var task_params = {
        taskid: taskid,
        userid: userid,
        taskname: taskname,
        description: description,
        tasktags: tasktags,
        status: 0,
        actions: actions,
        exttags: exttags,
        dsid: dsid, //新任务对应的dataset副本
        configid: configid
    };

    var flowconfig_params = {
        configid: configid,
        mark: taskobj.markobj.enable,
        inspect: taskobj.inspectobj.enable,
        rndinspect: taskobj.rndinspectobj.enable,
        accept: taskobj.acceptobj.enable,

        markusers: taskobj.markobj.users.join(','),
        inspectusers: taskobj.inspectobj.users.join(','),
        rndinspectusers: taskobj.rndinspectobj.users.join(','),
        acceptusers: taskobj.acceptobj.users.join(',')
    }


    taskdao.addtask(task_params, flowconfig_params, dslist_params).then(function (data) {
        console.log(data);

        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.post('/update', function (req, res, next) {
    var taskname = req.body.taskname;
    var description = req.body.description;
    var taskid = req.body.taskid;
    var userid = req.session.user.userid;
    var tasktags = req.body.tasktags;
    var exttags = req.body.exttags || '';
    var actions = req.body.actions;
    //var dsid = req.body.dsid;

    var params = {
        userid: userid,
        taskname: taskname,
        description: description,
        tasktags: tasktags,
        //status: 0,
        actions: actions,
        exttags: exttags
    };

    taskdao.update(params, taskid).then(function (data) {
        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

//发布任务
router.post('/publish', function (req, res, next) {
    var taskid = req.body.taskid;
    var dsid = '';
    var params = {
        taskid: taskid,
        status: 1 //任务进行中      
    };

    taskdao.update(params, taskid, dsid).then(function (data) {
        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

//验收任务
router.post('/accept', function (req, res, next) {
    var taskid = req.body.taskid;
    var dsid = req.body.dsid;

    var params = {
        taskid: taskid,
        status: 2 //任务完成
    };

    taskdao.update(params, taskid, dsid).then(function (data) {
        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.post('/flowupdate', function (req, res, next) {
    var taskobj = JSON.parse(req.body.params);

    var flowconfig_params = {
        mark: taskobj.markobj.enable,
        inspect: taskobj.inspectobj.enable,
        rndinspect: taskobj.rndinspectobj.enable,
        accept: taskobj.acceptobj.enable,

        markusers: taskobj.markobj.users.join(','),
        inspectusers: taskobj.inspectobj.users.join(','),
        rndinspectusers: taskobj.rndinspectobj.users.join(','),
        acceptusers: taskobj.acceptobj.users.join(',')
    }


    taskdao.updateflow(flowconfig_params, taskobj.configid).then(function (data) {
        console.log(data);

        if (data) {
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });
});

router.get('/mark', function (req, res, next) {
    res.render('task/mark', {
        action: 'markusers'
    });
});

router.get('/inspect', function (req, res, next) {
    res.render('task/inspect', {
        action: 'inspectusers'
    });
});

router.get('/rndinspect', function (req, res, next) {
    res.render('task/rndinspect', {
        action: 'rndinspectusers'
    });
});

router.get('/accept', function (req, res, next) {
    res.render('task/accept', {
        action: 'acceptusers'
    });
});


router.get('/actionlist', function (req, res, next) {
    var page = req.query.page || 1; //页号
    var limit = req.query.limit || 10; //页大小
    var action = req.query.action || ''; //查询条件
    var condition = {
        page: parseInt(page),
        limit: parseInt(limit),
        action: action,
        userid: req.session.user.userid
    };
    taskdao.getactionlist(condition).then(function (data) {
        res.json({
            "code": 0,
            "msg": "",
            data: data[0],
            count: data[1][0].total
        });
    }, function (error) {
        res.json({
            "code": 1,
            "msg": error.message
        });
    })
});

router.get('/classify_mark', function (req, res, next) {
    res.render('taskengine/classify_mark', {
        action: 'acceptusers'
    });
});

router.get('/classify_inspect', function (req, res, next) {
    res.render('taskengine/classify_inspect', {
        action: 'inspectusers'
    });
});

router.get('/classify_rndinspect', function (req, res, next) {
    res.render('taskengine/classify_rndinspect', {
        action: 'rndinspectusers'
    });
});

router.get('/aggreement', function (req, res, next) {
    res.render('taskengine/aggreement', {
        desc: req.query.desc
    });
});

//单分类标注
router.get('/single_mark', function (req, res, next) {
    res.render('taskengine/single_mark');
});
//单分类检查
router.get('/single_inspect', function (req, res, next) {
    res.render('taskengine/single_inspect');
});
//单分类抽查
router.get('/single_rndinspect', function (req, res, next) {
    res.render('taskengine/single_rndinspect');
});


//多分类标注
router.get('/muti_mark', function (req, res, next) {
    res.render('taskengine/muti_mark');
});
router.get('/muti_inspect', function (req, res, next) {
    res.render('taskengine/muti_inspect');
});
router.get('/muti_rndinspect', function (req, res, next) {
    res.render('taskengine/muti_rndinspect');
});
module.exports = router;